package my.sa;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

public class Compile {

	 
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String testPath = "E:/parttimework/毕业设计项目/Java静态分析/Test.java";
		Compile c = new Compile();
		String s = c.compile(testPath);
		//File file = new File(testPath);
		System.out.println(s);
		//System.out.println(file.getParent());
	}

	
	public String compile(String filePath){
		//String cmd = "D:/Java/jdk1.6.0_27/bin/javac.exe "+ filePath;
		String cmd = "javac "+ filePath;
		Process p;
		try {
			p = Runtime.getRuntime().exec(cmd);
			
			InputStream err = p.getErrorStream();
			InputStream in = p.getInputStream();
			p.waitFor();
			
			StringBuilder sb = new StringBuilder();
			byte[] b = new byte[1024*100];
			if(err.available()>0){
				int r = err.read(b);
				if(r>0)
				sb.append(new String(b,0,r,"GBK"));
			}
			if(in.available()>0){
				int r = in.read(b);
				if(r>0)
				sb.append(new String(b,0,r,"GBK"));
			}
			if(sb.length()>0){
				return sb.toString();
			}else{
				return "";
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} 
		return null;
	}
}
